package com.mooer.app;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import android.app.ActivityGroup;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ViewFlipper;

import com.mooer.MooerApplication;
import com.mooer.R;
import com.mooer.app.service.LocationService;
import com.mooer.app.service.LocationService.LocationServiceBinder;
import com.mooer.utils.ui.DialogUtils;

/**
 * 类说明
 * 
 * @author pengfei.xu
 * @version V1.0 创建时间：2011-4-29 下午05:44:02
 */
public class AppMain extends ActivityGroup {

	private static final String TAG = "HomeActivity";

	private ViewFlipper viewFlipper;
	LocationManager locationManager;
	MooerApplication mainApp = null;
	AlertDialog aDialog = null;
	LocationService service = null;
	Button button;
	boolean isBind = false;
	static List<String> items = new ArrayList<String>();

	private Handler handler = new Handler();

	Runnable monitor = new Runnable() {

		@Override
		public void run() {
			viewFlipper.setInAnimation(AnimationUtils.loadAnimation(
					AppMain.this, R.anim.left_in));
			viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(
					AppMain.this, R.anim.left_out));
			viewFlipper.showNext();
		}

	};

	ServiceConnection sConnection = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName componentName) {
			Log.i(TAG, "service disconnected");
			isBind = false;
		}

		@Override
		public void onServiceConnected(ComponentName componentName,
				IBinder iBinder) {
			Log.i(TAG, "service connected");
			LocationServiceBinder binder = (LocationServiceBinder) iBinder;
			service = binder.getService();
			isBind = true;
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		mainApp = (MooerApplication) getApplication();
		viewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);
		locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

		Intent intent = new Intent(this, Welcome.class);
		String id = UUID.randomUUID().toString();
		View welcomeView = getLocalActivityManager().startActivity(id, intent)
				.getDecorView();
		viewFlipper.addView(welcomeView);

		Intent intent2 = new Intent(this, Home.class);
		String id2 = UUID.randomUUID().toString();
		View homeView = getLocalActivityManager().startActivity(id2, intent2)
				.getDecorView();
		viewFlipper.addView(homeView);
		handler.postDelayed(monitor, 1200);

	}

	@Override
	protected void onResume() {
		super.onResume();
		if (!isBind) {
			checkNetwork();
			// checkGps();
		}
	}

	private void checkNetwork() {
		boolean isNetworkAvailable = false;
		final Intent intent = new Intent(AppMain.this, LocationService.class);
		ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

		if (connectivity != null) {
			NetworkInfo netinfo = connectivity.getActiveNetworkInfo();
			if (netinfo != null && netinfo.isConnected()) {
				isNetworkAvailable = true;
			}
		}
		if (!isNetworkAvailable) {

			Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("没有可用的网络")
					.setMessage(getText(R.string.network_tip))
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog,
										int which) {
									dialog.dismiss();
									Intent i = new Intent();
									i.setAction(Settings.ACTION_WIRELESS_SETTINGS);
									i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
									startActivity(i);
								}
							})
					.setNegativeButton("取消",
							new DialogInterface.OnClickListener() {
								public void onClick(
										DialogInterface dialoginterface, int i) {
									dialoginterface.dismiss();
									bindService(intent, sConnection,
											Context.BIND_AUTO_CREATE);
									// startService(intent);
								}
							}).show();
		} else {
			bindService(intent, sConnection, Context.BIND_AUTO_CREATE);
			// startService(intent);
		}
	}

	private void checkGps() {
		final Intent intent = new Intent(AppMain.this, LocationService.class);
		if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("GPS错误")
					.setMessage(getText(R.string.gps_tip))
					.setPositiveButton("是",
							new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog,
										int which) {
									dialog.dismiss();
									Intent i = new Intent();
									i.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
									i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
									startActivity(i);
								}
							})
					.setNegativeButton("否",
							new DialogInterface.OnClickListener() {
								public void onClick(
										DialogInterface dialoginterface, int i) {
									dialoginterface.dismiss();
									bindService(intent, sConnection,
											Context.BIND_AUTO_CREATE);
									// startService(intent);
								}
							}).show();
		} else {
			bindService(intent, sConnection, Context.BIND_AUTO_CREATE);
			// startService(intent);
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unbindService(sConnection);
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		switch (event.getAction()) {
		case KeyEvent.ACTION_DOWN:
			switch (event.getKeyCode()) {
			case KeyEvent.KEYCODE_BACK:
				DialogUtils.showExitDialog(this);
				return false;
			}
		}
		return super.dispatchKeyEvent(event);
	}

}
